<#assign tit>定金销售</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  function chgmtp(t) {
    document.getElementById("th_cdno").style.display = t==2?"":"none";
    document.getElementById("td_cdno").style.display = t==2?"":"none";
    var fm = getEl("fm_input");
    fm.custName.readOnly = t==2?true:false;
    fm.custName.className = t==2?"disable":"";
    fm.gender.readOnly = t==2?true:false;
    fm.gender.className = t==2?"disable":"";
    fm.idNumber.readOnly = t==2?true:false;
    fm.idNumber.className = t==2?"disable":"";
    getCardTypeList();
  }
  function chgtp() {
    var fm = getEl("fm_input");
    t = fm.earnestType.value;
    document.getElementById("tbd_tp1").style.display = t==1?"":"none";
    fm.promotionId.value = "";
    fm.cardType.value = "";
    getCardTypeList();
  }

  function getminfo(c) {
    if(c!="") {
      getJson("getMember?cardCode="+c, function(data){
        if(data.ret) {
          var mb = data.data;
          setc(mb);
        } else {
          window.alert(data.msg);
        }
      });
    }
  }
  function setc(mb) {
    var fm = getEl("fm_input");
    fm.cardCode.value = mb.card;
    fm.custName.value = mb.name;
    fm.gender.value = mb.gender;
    fm.idType.value = mb.idType;
    fm.idNumber.value = mb.idNumber;
    fm.mobile.value = mb.mobile;
  }

  function chkid(v) {
    if(v!="") {
      var fm = getEl("fm_input");
      getJson("getIdNumberMember?idt="+fm.idType.value+"&idn="+v, function(data){
        if(data.ret) {
          window.alert("会员信息已存在");
          var mb = data.data;
          setc(mb);
          if(mb.card!="") {
            fm.memberType.value = 2;
            chgmtp(2);
          }
        } else if(data.code==-8) {
          window.alert(data.msg);
        }
      });
    }
  }

  function chgct() {
    var fm = getEl("fm_input");
    var sl = fm.cardType;
    var pr = sl.options[sl.selectedIndex].getAttribute("pr");
    fm.cardPrice.value = pr?formatMoney(pr):"";
  }

  function getCardTypeList() {
    var fm = getEl("fm_input");
    if(fm.earnestType.value!="1") return;
    var st = fm.memberType.value;
    if(st=="2") st = "3";
    var op = "";
    if(fm.promotionId.value=="1") op += "p";
    getJson("getCardTypeList?saleType="+st+"&op="+op, function(data){
      if(data.ret) {
        fillCardType(data.data);
      } else {
        window.alert(data.msg);
      }
    });
  }
  function fillCardType(tl) {
    var fm = getEl("fm_input");
    var sl = fm.cardType;
    if(!sl) return;
    sl.length = 1;
    for(var i=0; i<tl.length; i++) {
      var op = new Option(tl[i].code+' - '+tl[i].name, tl[i].code);
      op.setAttribute("pr", tl[i].price)
      sl.options.add(op);
    }
    fm.cardType.value = "";
    chgct();
  }

  function save(fm) {
    if(!fm) fm = getEl("fm_input");
    if(fm.memberType.value==2) {
      if(fm.cardCode.value=="") {
        window.alert("请输入会员卡号");
        return;
      }
    }
    if(fm.idType.value=="1" && fm.idNumber.value!="" && !isIdNumber(fm.idNumber.value)) {
      window.alert("请输入正确的身份证号码");
      return;
    }
    var mm = fm.payAmount.value;
    post(fm, function(){
      openWin("getPay?money="+mm, 500, 400);
      return false;
    });
    return false;
  }
  function payOk(vv) {
    var fm = getEl("fm_input");
    fm.payls.value = vv;
    postForm(fm.action, fm, saveRet);
  }
  function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) {
      getEl("fm_input").reset();
      chgmtp(1);
      chgtp();
    }
  }
</script>
<div style="width:600px;">

  <form id="fm_input" method="post" action="_add" onsubmit="return save(this);">
    <input type="hidden" name="payls" value="">
    <div class="kbox">
      <table class="input w4">
        <tr>
          <th>会员类型</th>
          <td><select name="memberType" onchange="chgmtp(this.value);">
            <option value="1">1-新会员</option>
            <option value="2">2-老会员</option>
          </select></td>
          <th id="th_cdno" style="display:none;">会员卡号</th>
          <td id="td_cdno" style="display:none;"><input type="text" name="cardCode" value="" onchange="getminfo(this.value);" maxlength="20"></td>
        </tr>
        <tr>
          <th width="85">会员姓名</th>
          <td width="160"><input type="text" name="custName" id="ipt_custName" value="" req="会员姓名"></td>
          <th width="85">性别</th>
          <td width="160"><select name="gender"><#if gender??><#list gender as t><option value="${t.id}">${t.name}</option></#list></#if></select></td>
        </tr>
        <tr>
          <th>证件号</th>
          <td colspan="3"><select name="idType" req="证件类型"><#if idType??><#list idType as t><option value="${t.id}">${t.name}</option></#list></#if></select>
            <input type="text" name="idNumber" value="" onchange="chkid(this.value);" style="width:240px;"></td>
        </tr>
        <tr>
          <th>手机号码</th>
          <td><input type="text" name="mobile" value="" req="手机号码"></td>
          <th></th>
          <td></td>
        </tr>
        <tr>
          <th>定金类型</th>
          <td><select name="earnestType" onchange="chgtp();"><#if earnestType??><#list earnestType as t><option value="${t.id}">${t.name}</option></#list></#if></select></td>
          <th>定金金额</th>
          <td><input type="text" name="payAmount" value="" req="定金金额"></td>
        </tr>

        <tbody id="tbd_tp1">
        <tr>
          <th>营销活动</th>
          <!--td colspan="3"><select name="promotionId" class="w2"><option value="">--请选择--</option><#if promotionList??><#list promotionList as t><option value="${t.promotionId}">${t.chName}</option></#list></#if></select></td-->
          <td colspan="3"><select name="promotionId" onchange="getCardTypeList();" class="w2"><option value="">不参加营销活动</option><option value="1">参加营销活动</option></select></td>
        </tr>
        <tr>
          <th>卡种类型</th>
          <td colspan="3"><select name="cardType" class="w2" onchange="chgct();"><option value="" pr="">--请选择--</option><#if cardTypeList??><#list cardTypeList as t><option value="${t.code}" pr="${t.price}">${t.code} - ${t.chName}</option></#list></#if></select></td>
        </tr>
        <tr>
          <th>卡种价格</th>
          <td colspan="3"><input type="text" name="cardPrice" value="" readonly class="disable"></td>
        </tr>
        </tbody>

        <tr>
          <th>备注</th>
          <td colspan="3"><textarea name="note"></textarea></td>
        </tr>
      </table>
    </div>
    <div class="button"><input type="button" value="确认并提交" onclick="save();"></div>
  </form>
</div>
<script>
  <#if earnType??>getEl("fm_input").earnestType.value="${earnType}";</#if>
  chgtp();
</script>
</@page.html>